import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import path from 'node:path'
import fs from 'fs'

interface PageOut {
  [key: string]: string
}

const getPages = (): PageOut => {
  const dir = path.resolve(__dirname, '.');
  const files = fs.readdirSync(dir);
  const pages = {};
  for (const file of files) {
    if (!file.endsWith('.html')) continue;
    const name = file.replace(/\.html$/, '');
    pages[`${name}`] = path.resolve(__dirname, file);
  }
  return pages;
}

// 全局 scss 资源
const scssResources: string[] = []
fs.readdirSync('src/styles').forEach((dirname) => {
  if (fs.statSync(`src/styles/${dirname}`).isFile()) {
    scssResources.push(`@import "./src/styles/${dirname}";`)
  }
})

console.log(scssResources)


export default defineConfig({
  plugins: [UnoCSS()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  build: {
    rollupOptions: {
      input: getPages()
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: scssResources.join('')
      },
    },
  },
})